# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.StudentInfo.StudentInfoAtt import StudentInfoAtt
from LBT.GeneralOperation.StudentOperation import StudentOperation
from selenium.webdriver.common.keys import Keys


class StudentInfo(lbtlogin, StudentInfoAtt):
    '''在园幼儿园模块
    1、随意查询一个学生，然后核对页面信息和详情是否一致。
    2、添加一个学生，通过组合所有查询条件查询学生并核对详细信息是否一致。
    3、随机一个学生，修改档案，然后查询校验
    4、退学，然后数据库校验
    5、批量导出，然后核对数据总条数和第一条及最后一条数据是否一致。
    '''

    @lbtlogin.loginFun
    def test_exportstudent(self):
        """
        批量导出，然后核对数据总条数和第一条及最后一条数据是否一致。
        :return:
        """
        self.docstr = '批量导出，然后核对数据总条数和第一条及最后一条数据是否一致。'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        # 拿到第一个孩子的信息
        infodict = stuopr.getstudentinfo(firststudent)
        listinfo = {}
        infodict["phone"] = ""
        listinfo["first"] = infodict
        pages = self.find_elements_by_href(dr, self.studentinfo_page_href)
        pages[pages.__len__() - 2].location_once_scrolled_into_view
        pages[pages.__len__() - 2].click()
        infos = dr.find_elements_by_xpath(self.studentinfo_students_xpath)
        laststudent = infos[infos.__len__() - 1]
        # 拿到最后一个孩子的信息
        infodictlast = stuopr.getstudentinfo(laststudent)
        infodictlast["phone"] = ""
        listinfo["last"] = infodictlast
        # 拿到总数
        totalnum = int(dr.find_element_by_xpath(self.studentinfo_totalnum_xpath).text)
        listinfo["total"] = totalnum
        # 获取验证码，进行导出操作
        dr.find_element_by_css_selector(self.studentinfo_export_css).click()
        dr.find_element_by_id(self.studentinfo_getsmscode_id).click()
        sql = "select top 1 smscontent from smscode  where phone in(SELECT t.SchoolmasterPhone FROM School t LEFT JOIN dbo.Users t1 ON t1.SchoolID = t.SchoolID AND t1.Phone = t.SchoolmasterPhone AND t1.UserType = 1 AND t1.State = 1 WHERE t.Schoolname = '" + self.lbt_schoolname_text + "') order by id desc"
        code = self.selectlbt(sql).split("(")[0]
        dr.find_element_by_id(self.studentinfo_smscode_id).send_keys(code)
        self.deleteAllFiles(self.defaultdownpath)
        sql1 = "delete from smscode where phone = (select schoolmasterphone from school where schoolname = '" + self.lbt_schoolname_text + "') "
        self.deleteAndUpdatelbt(sql1)
        dr.find_element_by_class_name(self.studentinfo_exportconfire_class).click()
        list = self.readExcle(self.defaultdownpath)
        self.assertTrue(list.__len__() == int(listinfo["total"]) + 1)
        for v1 in infodict.values():
            self.assertTrue(str(list).__contains__(v1))
        for v2 in infodictlast.values():
            self.assertTrue(str(list).__contains__(v2))

    @lbtlogin.loginFun
    def test_leavetudent(self):
        """
        退学，然后数据库校验
        :return:
        """
        self.docstr = '退学，然后数据库校验'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        dr.find_element_by_id(self.studentinfo_name_id).send_keys("添加")
        dr.find_element_by_xpath(self.studentinfo_selectbutton_xpath).click()
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict = stuopr.getstudentinfo(firststudent)
        dr.find_elements_by_css_selector(self.studentinfo_leavestus_css)[0].click()
        dr.find_element_by_class_name(self.studentinfo_addstudentbtn_class).click()
        infodict["class"] = str(infodict["class"]).split("(")[0]
        sql = "select u.state from users u inner join student st on st.studentid = u.usersid inner join eduunit edu on edu.eduunitid = st.eduunitid where u.phone = '" + \
              infodict["phone"] + "' and u.usertype =2 and u.name = '" + infodict[
                  "name"] + "' and edu.level=5 and edu.eduunitname = '" + infodict["class"] + "'"
        self.assertTrue(int(self.selectlbt(sql)) == 2)

    @lbtlogin.loginFun
    def test_editrandomstudent(self):
        """
        随机一个学生，修改档案，然后查询校验
        :return:
        """
        self.docstr = '随机一个学生，修改档案，然后查询校验'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict = stuopr.getstudentinfo(firststudent)
        dr.find_elements_by_css_selector(self.studentinfo_updatebtns_css)[0].click()
        name = "修改" + self.getRandomStr(4)
        self.clearAndSendKeyForText(dr.find_element_by_id(self.studentinfo_studentNameupdate_id), name)
        dr.find_element_by_class_name(self.studentinfo_addstudentbtn_class).click()
        dr.find_element_by_id(self.studentinfo_name_id).send_keys(name)
        dr.find_element_by_xpath(self.studentinfo_selectbutton_xpath).click()
        newfirststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict2 = stuopr.getstudentinfo(newfirststudent)
        self.assertEqual(infodict2["name"], name)
        self.assertTrue(str(infodict["class"]) == str(infodict2["class"]))
        self.assertTrue(str(infodict["sex"]) == str(infodict2["sex"]))
        self.assertTrue(str(infodict["phone"]) == str(infodict2["phone"]))

    @lbtlogin.loginFun
    def test_selectrandomstudent(self):
        """
        随意查询一个学生，然后核对页面信息和详情是否一致。
        :return:
        """
        self.docstr = '随意查询一个学生，然后核对页面信息和详情是否一致。'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        stuopr = StudentOperation(dr)
        firststudent = dr.find_elements_by_xpath(self.studentinfo_students_xpath)[0]
        infodict = stuopr.getstudentinfo(firststudent)
        dr.find_elements_by_css_selector(self.studentinfo_childdetailbutton_css)[0].click()
        firststudent.find_elements_by_tag_name("button")[0].click()
        self.webdriverwait(dr, dr.find_element_by_css_selector(self.studentinfo_childdetailinfo_css))
        childjichu = dr.find_element_by_css_selector(self.studentinfo_childdetailinfo_css)
        details = str(childjichu.text).split("\n")
        detaildict = {}
        detaildict['name'] = str(details[2])
        detaildict['class'] = str(details[4])
        detaildict['sex'] = str(details[6])
        detaildict['phone'] = str(details[10])
        self.assertTrue(infodict == detaildict)

    @lbtlogin.loginFun
    def test_addstudent(self):
        """
        添加一个学生，通过组合所有查询条件查询学生并核对详细信息是否一致。
        档案状态为未完成。
        :return:
        """
        self.docstr = '添加一个学生，通过组合所有查询条件查询学生并核对详细信息是否一致'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentinfo_navigation_class, self.studentinfo_studentinfo_url,
                                      self.student_topnavigation_class)
        dr.find_element_by_css_selector(self.studentinfo_enterGarden_css).click()
        # 记录入校登记信息姓名+性别+监护人关系+监护人手机号
        self.webdriverwait(dr, dr.find_element_by_id(self.studentinfo_enterGardenAdd_id))
        stuopr = StudentOperation(dr)
        name = "添加" + self.getRandomStr(4)
        dr.find_element_by_id(self.studentinfo_studentName_id).send_keys(name)
        dr.find_element_by_id(self.studentinfo_studentClass_id).send_keys(Keys.ARROW_DOWN)
        classname = dr.find_elements_by_xpath(self.studentinfo_studentClasslist_xpath)[1].text
        self.clickByjs(dr, dr.find_element_by_id(self.studentinfo_addstudentsexgirl_id))
        sex = '女'
        dr.find_element_by_id(self.studentinfo_addfirstrelation_id).click()
        releation = dr.find_elements_by_xpath(self.studentinfo_addfirstrelationlist_xpath)[1].text
        dr.find_elements_by_xpath(self.studentinfo_addfirstrelationlist_xpath)[1].click()
        dr.find_element_by_id(self.studentinfo_addstudentFirstPhone_id).send_keys(self.lbtUserName)
        dr.find_element_by_class_name(self.studentinfo_addstudentbtn_class).click()
        dr.find_element_by_xpath(self.studentinfo_successbtn_xpath).click()
        # 查询刚才的学生
        stuopr.selectWithInput(dr, 0, 0, classname)
        stuopr.selectWithOutInput(dr, 1, '未完成')
        dr.find_element_by_id(self.studentinfo_name_id).send_keys(name)
        dr.find_element_by_id(self.studentinfo_phone_id).send_keys(self.lbtUserName)
        dr.find_element_by_xpath(self.studentinfo_selectbutton_xpath).click()
        studentinfos = dr.find_elements_by_xpath(self.studentinfo_students_xpath)
        self.assertTrue(studentinfos.__len__() == 1)
        firststudent = studentinfos[0]
        infodict = stuopr.getstudentinfo(firststudent)
        self.assertEqual(infodict["name"], name)
        self.assertTrue(str(infodict["class"]).__contains__(classname))
        self.assertTrue(str(infodict["sex"]) == sex)
        self.assertTrue(str(infodict["phone"]) == self.lbtUserName)
